ARG PHP_BASE=php:8.4-rc-fpm-alpine

FROM alpine:3.19 AS fetcher

ARG API_REPO=https://gitee.com/yiliaocrm/api
ARG API_REF=master
ARG FRONTEND_REPO=https://gitee.com/yiliaocrm/frontend-dist
ARG FRONTEND_REF=master

RUN apk add --no-cache git p7zip

WORKDIR /src

RUN git clone --depth=1 --branch "${API_REF}" "${API_REPO}" backend
RUN git clone --depth=1 --branch "${FRONTEND_REF}" "${FRONTEND_REPO}" frontend

RUN mkdir -p backend/public/dist \
    && 7z x frontend/dist.7z -o/tmp/frontend \
    && for dir in web his install admin; do \
        if [ -d "/tmp/frontend/${dir}" ]; then \
          cp -r "/tmp/frontend/${dir}" backend/public/dist/; \
        fi; \
       done

FROM ${PHP_BASE} AS vendor

WORKDIR /app

COPY --from=fetcher /src/backend /app

# Install composer compatible with PHP 8.4
COPY --from=composer:2.7 /usr/bin/composer /usr/local/bin/composer

ENV COMPOSER_ALLOW_SUPERUSER=1

RUN apk add --no-cache git unzip \
    && composer install --no-dev --prefer-dist --no-progress --no-interaction --optimize-autoloader --ignore-platform-reqs \
    && if [ -f .env.example ] && [ ! -f .env ]; then cp .env.example .env; fi

FROM ${PHP_BASE} AS runtime

ARG TZ=Asia/Shanghai

ENV APP_ENV=production \
    APP_DEBUG=false \
    PHP_MEMORY_LIMIT=512M \
    PHP_MAX_EXECUTION_TIME=120 \
    PHP_UPLOAD_MAX_FILESIZE=10M \
    PHP_POST_MAX_SIZE=12M

RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        icu-dev \
        libpng-dev \
        libjpeg-turbo-dev \
        freetype-dev \
        libzip-dev \
        libxml2-dev \
        oniguruma-dev \
        sqlite-dev \
        imagemagick-dev \
        readline-dev \
    && apk add --no-cache \
        bash \
        shadow \
        tzdata \
        curl \
        git \
        icu-libs \
        libzip \
        imagemagick \
        supervisor \
        nginx \
        redis \
        mysql-client \
        p7zip \
        gettext \
        su-exec \
        netcat-openbsd \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" \
        bcmath \
        dom \
        exif \
        gd \
        mbstring \
        pcntl \
        pdo_mysql \
        pdo_sqlite \
        simplexml \
        xml \
        xmlreader \
        xmlwriter \
        opcache \
        zip \
        sqlite3 \
    && cd /tmp \
    && pecl install -o -f igbinary \
    && pecl install -o -f redis \
    && docker-php-ext-enable igbinary redis \
    && apk del .build-deps \
    && rm -rf /tmp/pear

RUN ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime && echo "${TZ}" > /etc/timezone

RUN addgroup -g 1000 app && adduser -D -G app -g app -u 1000 app

WORKDIR /var/www/html

COPY --from=vendor /app /var/www/html

COPY docker/php.ini /etc/his/php.ini.template
COPY docker/php-fpm.conf /etc/his/php-fpm.conf.template
COPY docker/entrypoint.sh /usr/local/bin/entrypoint
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/nginx.conf /etc/his/nginx.conf.template

RUN mkdir -p storage bootstrap/cache /run/nginx /run/php /var/log/supervisor /var/log/nginx /var/lib/redis \
    && chown -R app:app storage bootstrap/cache \
    && chown -R redis:redis /var/lib/redis \
    && chown -R app:app /var/www/html \
    && chmod +x /usr/local/bin/entrypoint

EXPOSE 80

ENTRYPOINT ["/usr/local/bin/entrypoint"]
CMD ["start"]
